package eai.tp2.lpco.wsclients.camerasupplier;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import eai.tp2.lpco.database.DigitalCamera;

@Stateless
public class CameraSupplierClientBean implements ICameraSupplierClient {

	private List<DigitalCamera> parseResult(String result) {
		try {
			result = new String(result.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			System.out.println("[Error] Error enconding to UTF-8");
		}
		List<Element> elemResults = new ArrayList<Element>();
		Document xmlResult = parseStringToXML(result);
		Element root = xmlResult.getRootElement();
		elemResults = (List<Element>) root.getChildren();
		List<DigitalCamera> camResults = new ArrayList<DigitalCamera>();
		for (Element ElemRes : elemResults)
			camResults.add(parseElement(ElemRes));
		return camResults;
	}

	private Document parseStringToXML(String xml) {
		ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes());
		SAXBuilder parser = new SAXBuilder();
		Document doc = null;
		try {
			doc = parser.build(in);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return doc;
	}

	private DigitalCamera parseElement(Element cam) {
		DigitalCamera camera = new DigitalCamera();
		camera.setModelName(cam.getChild("name").getValue());
		camera.setBrandName(cam.getChild("brand").getValue());
		camera.setDescription(cam.getChild("description").getValue());
		camera.setDate(parseDate(cam.getChild("date").getValue()));
		camera.setEffectivePixels(cam.getChild("effective_pixels").getValue());
		int id = Integer.parseInt(cam.getAttributeValue("id"));
		camera.setCameraId(id);
		camera.setImageLink(cam.getChild("picture_link").getValue());
		camera.setImageRatio(cam.getChild("image_ratio").getValue());
		camera.setIsoRating(cam.getChild("iso_rating").getValue());
		camera.setMaxRes(cam.getChild("max_res").getValue());
		camera.setMinRes(cam.getChild("min_res").getValue());
		camera.setMaxShutter(cam.getChild("max_shutter").getValue());
		camera.setMinShutter(cam.getChild("min_shutter").getValue());
		camera.setReviewLink(cam.getChild("review_link").getValue());
		camera.setSensorSize(cam.getChild("sensor_size").getValue());
		camera.setPrice(Integer.valueOf(cam.getChild("price").getValue()));

		return camera;
	}

	/**
	 * The date value come as in the format yyyy-mm-dd
	 * 
	 * @param value
	 * @return
	 */
	private Date parseDate(String dateString) {

		String[] x = dateString.split("-");

		Date date = new Date(Integer.parseInt(x[0]), Integer.parseInt(x[1]),
				Integer.parseInt(x[2]));

		return date;
	}

	@Override
	public List<DigitalCamera> executeQuery(String query) {
		List<DigitalCamera> camerasResult = new ArrayList<DigitalCamera>();

		CameraSupplierService client = new CameraSupplierService();
		String result = client.getCameraSupplierPort().search(query.trim());
		camerasResult = this.parseResult(result);

		return camerasResult;
	}

}
